/**
 * Author: luoqi
 * Created Date: 2025-11-13 01:08:7
 * Last Modified: 2025-11-14 21:03:16
 * Modified By: luoqi at <**@****>
 * Copyright (c) 2025 <*****>
 * Description:
 */

#include <bsp.h>
#include <cli.h>
#include <stdlib.h>
#include <io_port.h>
#include "dbg.h"

static int cmd_io_set(int argc, char **argv)
{
    if(argc != 3) {
        return -1;
    }
    int state = strtol(argv[2], NULL, 10);
    if((state != 0) && (state != 1)) {
        return -1;
    }
    if(ISARG(argv[1], "blink")) {
        io_set(GPO_BLINK, state);
    } else if(ISARG(argv[1], "drv")) {
        io_set(GPO_DRV_ENA, state);
    } else if(ISARG(argv[1], "a15")) {
        io_set(GPO_A15, state);
    } else {
        return -1;
    }
    return 0;
}

static int cmd_io_get(int argc, char **argv)
{
    if(argc != 2) {
        return -1;
    }
    if(ISARG(argv[1], "b8")) {
        IOState state = io_get(GPI_B8);
        QSH("b8: %d\n", (int)state);
    } else if(ISARG(argv[1], "b9")) {
        IOState state = io_get(GPI_B9);
        QSH("b9: %d\n", (int)state);
    } else {
        return -1;
    }
    return 0;
}

static QCliArgsTable table[] = {
    {"set", cmd_io_set, "[<blink/drv/a15> <0/1>], Set IO state"},
    {"get", cmd_io_get, "[<c13>], Get IO state"},
};

static int cmd_io_hdl(int argc, char **argv)
{
    if(argc == 1) {
        cli_help(table, sizeof(table));
        return 0;
    }
    CLI_ARGS_EXEC(argc, argv, table);
}

int cmd_io_init(void)
{
    cli_add("io", cmd_io_hdl, "IO command");
    return 0;
}
